package com.mes.loco.aps.server.controller.aps;

import com.iplant.aps.controller.BaseController;
import com.iplant.aps.service.APSService;
import com.iplant.aps.service.po.ServiceResult;
import com.iplant.aps.service.po.aps.APSPartPointWeekPlan;
import com.iplant.aps.service.po.bms.BMSEmployee;
import com.iplant.aps.service.po.fpc.FPCRoutePartPoint;
import com.iplant.aps.service.utils.CloneTool;
import com.iplant.aps.service.utils.StringUtils;
import com.iplant.aps.utils.RetCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author PengYouWang
 * 2023-2-13 14:23:27
 */
@RestController
@RequestMapping("/api/APSPartPointWeekPlan")
public class APSPartPointWeekPlanController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(APSPartPointWeekPlanController.class);

    final
    APSService wAPSService;

    public APSPartPointWeekPlanController(APSService wAPSService) {
        this.wAPSService = wAPSService;
    }

    /**
     * 根据订单、工位查询BOP工序列表
     */
    @GetMapping("/BopPartPointList")
    public Object BopPartPointList(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wOrderID = StringUtils.parseInt(request.getParameter("OrderID"));
            int wPartID = StringUtils.parseInt(request.getParameter("PartID"));

            if (wOrderID <= 0 || wPartID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<List<FPCRoutePartPoint>> wServiceResult =
                    wAPSService.APS_QueryBopPartPointList(wLoginUser,
                            wOrderID, wPartID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
                for (String wKey : wServiceResult.CustomResult.keySet()) {
                    this.SetResult(wResult, wKey, wServiceResult.CustomResult.get(wKey));
                }
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 查单条
     */
    @GetMapping("/Info")
    public Object Info(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            int wID = StringUtils.parseInt(request.getParameter("ID"));

            ServiceResult<APSPartPointWeekPlan> wServiceResult = wAPSService.APS_QueryPartPointWeekPlan(wLoginUser,
                    wID);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 条件查询
     */
    @GetMapping("/All")
    public Object All(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            int wOrderID = StringUtils.parseInt(request.getParameter("OrderID"));
            int wPartID = StringUtils.parseInt(request.getParameter("PartID"));
            int wPartPointID = StringUtils.parseInt(request.getParameter("PartPointID"));

            ServiceResult<List<APSPartPointWeekPlan>> wServiceResult =
                    wAPSService.APS_QueryPartPointWeekPlanList(wLoginUser,
                            wOrderID, wPartID, wPartPointID);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 批量保存
     */
    @PostMapping("/SaveList")
    public Object SaveList(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            if (!wParam.containsKey("data"))
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);

            List<APSPartPointWeekPlan> wAPSPartPointWeekPlanList = CloneTool.CloneArray(wParam.get("data"),
                    APSPartPointWeekPlan.class);

            if (wAPSPartPointWeekPlanList == null || wAPSPartPointWeekPlanList.size() <= 0)
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);

            ServiceResult<Integer> wServiceResult = wAPSService.APS_SavePartPointWeekPlanList(wLoginUser,
                    wAPSPartPointWeekPlanList);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }
}
